namespace core {
    export class StageUtils extends BaseClass {

        //唯一的_uiStage作为UI的根容器
        private static _uiStage: eui.UILayer;

        public constructor() {
            super()
            if (StageUtils._uiStage == null) {
                StageUtils._uiStage = new eui.UILayer();
                StageUtils._uiStage.percentHeight = 100;
                StageUtils._uiStage.percentWidth = 100;
                StageUtils._uiStage.touchEnabled = false;

                this.getStage().addChild(StageUtils._uiStage);
            }
        }

        //获取游戏的全局Stage对象
        public getStage(): egret.Stage {
            return egret.MainContext.instance.stage;
        }

        //获取游戏宽高
        public getHeight(): number {
            return this.getStage().stageHeight;
        }
        public getWidth(): number {
            return this.getStage().stageWidth;
        }

        //指定此对象的子项以及子孙项是否接收鼠标/触摸事件
        public setTouchChildren(value: boolean): void {
            this.getStage().touchChildren = value;
        }

        //设置同时可触发几个点击事件
        public setMaxTouches(value: number): void {
            this.getStage().maxTouches = value;
        }

        //设置帧频
        public setFrameRate(value: number): void {
            this.getStage().frameRate = value;
        }

        //设置适配方式
        public setScaleMode(value: string): void {
            this.getStage().scaleMode = value;
        }

        //获取根UI容器
        public getUIStage(): eui.UILayer {
            return StageUtils._uiStage;
        }

        /*
        全屏适配
        */
        private designWidth: number;
        private designHeight: number;
        private resizeCallback: Function;

        public startFullscreenAdaptation(designWidth: number, designHeight: number, resizeCallback: Function): void {
            this.designWidth = designWidth;
            this.designHeight = designHeight;
            this.resizeCallback = resizeCallback;
            this.stageOnResize();
        }

        private stageOnResize(): void {
            this.getStage().removeEventListener(egret.Event.RESIZE, this.stageOnResize, this);

            var designWidth: number = this.designWidth;
            var designHeight: number = this.designHeight;
            var clientWidth: number = window.innerWidth;
            var clientHeight: number = window.innerHeight;
            var a: number = clientWidth / clientHeight;
            var b: number = designWidth / designHeight;
            var c: number = a / b;
            if (a > b) {
                var c1 = c;
                var c2 = c;
                designWidth = Math.floor(designWidth * c1);
                designHeight = Math.floor(designHeight * c2);
            }
            this.getStage().setContentSize(designWidth, designHeight);
            this.resizeCallback && this.resizeCallback();

            this.getStage().addEventListener(egret.Event.RESIZE, this.stageOnResize, this);
        }
    }
}